//
//  LogInRegisterViewController.m
//  myMedicine1.0
//
//  Created by pollysoft on 16/7/15.
//  Copyright © 2016年 microi. All rights reserved.
//

#import "LogInRegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import"timeController.h"
@interface LogInRegisterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *userPassword;
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (IBAction)userNameTextField_DidEndOnExit:(id)sender;
- (IBAction)userPasswordTextField_DidEndOnExit:(id)sender;

@end

@implementation LogInRegisterViewController
- (IBAction)LogInActin:(id)sender {
    
    if (_userName.text == nil) {
        // 弹个错误框
        return;
    }
    if (_userPassword.text == nil) {
        // 弹个错误框
        return;
    }
    [AVUser logInWithUsernameInBackground:_userName.text password:_userPassword.text block:^(AVUser *user, NSError *error) {
        if (user != nil) {
            [self performSegueWithIdentifier:@"toTimeControllerSegue" sender:nil];
        } else {
            // 弹个错误框
        }
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _userPassword.delegate = self;
    _userName.delegate =self;
    //判断用户是否已经登录
    if ([AVUser currentUser]!=nil) {
        [self performSegueWithIdentifier:@"toTimeControllerSegue" sender:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//弹出键盘时，假如输入框被挡住，则输入框上移
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    CGFloat offSet = self.view.frame.size.height - (textField.frame.origin.y + textField.frame.size.height + 216 +50);
    if (offSet <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.view.frame;
            frame.origin.y = offSet;
            self.view.frame = frame;
            
            
        }];
    }
    
    return YES;
}

//实现回收键盘时，输入框恢复原来的位置
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:YES animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = 0.0;
        self.view.frame = frame;
        
    }];
    
    return YES;
}



- (IBAction)userNameTextField_DidEndOnExit:(id)sender {//将焦点移至下一个
    
    [_userPassword becomeFirstResponder];
    
    
}

- (IBAction)userPasswordTextField_DidEndOnExit:(id)sender {//隐藏软件盘
    [sender resignFirstResponder];
}

//点击背景隐藏键盘

- (IBAction)touchDown:(id)sender {
    
    [[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

@end
